package org.droid.campusorder.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelperAboutSearch extends SQLiteOpenHelper {

	// 数据库版本
	private static final int VERSION = 1;
	private static final String TBNAME = "SearchContent";
	// 新建一个表
	String sql = "create table if not exists " + TBNAME
			+ "(_id INTEGER PRIMARY KEY AUTOINCREMENT,content TEXT)";

	public MyDBHelperAboutSearch(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	public MyDBHelperAboutSearch(Context context, String name, int version) {
		this(context, name, null, version);
	}

	public MyDBHelperAboutSearch(Context context, String name) {
		this(context, name, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	/**
	 * 添加数据
	 */
	public long insert(String content) {
		SQLiteDatabase db = getWritableDatabase();// 获取可写SQLiteDatabase对象
		// ContentValues类似map，存入的是键值对
		ContentValues contentValues = new ContentValues();
		contentValues.put("content", content);
		return db.insert(TBNAME, null, contentValues);
	}

	/**
	 * 删除记录
	 * 
	 * @param _id
	 */
	public void delete(String _id) {
		SQLiteDatabase db = getWritableDatabase();
		db.delete(TBNAME, "_id=?", new String[] { _id });
	}

	/**
	 * 删除全部记录
	 * 
	 * @param _id
	 */
	public void deleteAllData() {
		SQLiteDatabase db = getWritableDatabase();
		db.delete(TBNAME, null, null);
	}

	/**
	 * 查询所有数据
	 * 
	 * @return Cursor
	 */
	public Cursor query() {
		SQLiteDatabase db = getReadableDatabase();
		return db.query(TBNAME, null, null, null, null, null, null);
		
	}
}
